<?php
//echo time();
//包含基本文件
include_once('./common.php');
include_once(S_ROOT.'./source/function_cp.php');
include_once S_ROOT.'./uc_client/client.php';

//------这里填写你自己的插件功能代码 开始------
/*
经常用到的基本变量、函数含义如下：
$_SGLOBAL['supe_uid'] 	该变量为当前用户的UID
$_SGLOBAL['supe_username'] 	该变量为当前用户的名字
$_SGLOBAL['db'] 	数据库操作对象
showmessage(message, url)	该函数为显示跳转信息： message 为信息， url 为跳转url
ckstart(start, perpage)	检查输入的start是否合法：start 为开始数，perpage为每页显示数
smulti(start, perpage, count, url)	获取分页的函数：start 为开始数，perpage为每页显示数，count为当前列表数，url为页面url
$space = getspace(uid)	该函数为获取当前用户的空间信息：uid为用户uid
其中获取到的$space，常用的有以下几个：
$space['uid']  用户UID
$space['username'] 用户名
$space['frienduid'] 用户的好友UID列表，以逗号分割。例如：1,5,6,7
添加个人动态的方法：
include_once(S_ROOT.'./source/function_cp.php');
$icon = 'mylink';	//个人动态前面的图标英文名，随意命名，需要修改css支持
$title_template = '{actor} 更新了自己的 <a href="space.php?&do=mylink">友情链接</a>';	//标题内容，支持html，其中 {actor} 为内置变量，表示当前人名
feed_add($icon, $title_template);
*/

//检查是否登录
if(empty($_SGLOBAL['supe_uid'])) {
	showmessage('请先登录后，再访问', 'do.php?ac=login');
}

//获取当前用户的空间信息
$space = getspace($_SGLOBAL['supe_uid']);

//列表变量
$list = array();
//数量
$count = 0;
//每页显示个数
$perpage = 5;
//分页开始
$start = empty($_GET['start'])?0:intval($_GET['start']);

//检查输入的start是否合法
ckstart($start, $perpage);



//页面的标题
$_TPL['titles'] = array($space['username'], '友情链接');
//页面不同的标签激活
$actives = array($_GET['action']=>' class="active"');


//根据不同的方法做不同的处理

//默认的方法
if(empty($_GET['action'])) $_GET['action'] = 'all';

if($_GET['action'] == 'all') {
	$perpage = 5;
	$query = $_SGLOBAL['db']->query("SELECT * FROM  uchome_link  WHERE uid !=0 limit $start ,$perpage");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[] = $value;
		//count累加
		$count++;
	}
	if(empty($list)) {
		showmessage('现在不存在任何友情链接', 'space.php?uid=$space[uid]&do=mylink');
	}
	$num = $_SGLOBAL['db']->num_rows($query) ;
	//echo $num;
	//分页处理
	//$multi = multi($num, $perpage, 1, "space.php?uid=$space[uid]&do=mylink&action=$_GET[action]");
	//echo $multi;
	$smulti = smulti($start, $perpage, $count, "space.php?uid=$space[uid]&do=mylink&action=$_GET[action]");
	//echo $smulti;
    include template('mylink');
} elseif($_GET['action'] == 'me') {
	
	$query = $_SGLOBAL['db']->query("SELECT * FROM uchome_link WHERE uid='$_SGLOBAL[supe_uid] limit $start ,$perpage'");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[] = $value;
	}
	if(empty($list)) {
		showmessage('现在不存在任何友情链接', 'space.php?uid=$space[uid]&do=mylink');
	}
	$smulti = smulti($start, $perpage, $count, "space.php?uid=$space[uid]&do=mylink&action=$_GET[action]");
	include template('mylink_me');
} elseif($_GET['action'] == 'update') {

	$id = $_POST['id'];
	$name = $_POST['name'];
	//exit($name);
	$url = $_POST['message'];
	//exit($url);
	$query = $_SGLOBAL['db']->query("update uchome_link set name = '$name', url = '$url' where id = '$id'");	
	$rowsAffected = $_SGLOBAL['db']->affected_rows();
	if ($rowsAffected >0 ) {
		showmessage('更新友情链接成功', 'space.php?uid=$space[uid]&do=mylink');
	}else{
		showmessage('更新友情链接失败', 'space.php?uid=$space[uid]&do=mylink');
	}

} elseif($_GET['action'] == 'sort') {
   include template('mylink');
} elseif($_GET['action'] == 'cp_sort') {

	$fieldname = $_POST['fieldname'] ? $_POST['fieldname'] : $_GET['fieldname'];
	$sortname = $_POST['sortname'] ? $_POST['sortname'] : $_GET['sortname'];
	$query = $_SGLOBAL['db']->query("SELECT * FROM  uchome_link order by $fieldname $sortname  LIMIT $start,$perpage");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[] = $value;
		//count累加
		$count++;
	}
	if(empty($list)) {
		showmessage('现在不存在任何友情链接', 'space.php?uid=$space[uid]&do=mylink');
	}
	//分页处理
	$multi = smulti($start, $perpage, $count, "space.php?uid=$space[uid]&fieldname=$fieldname&sortname=$sortname&do=mylink&action=$_GET[action]");
   include template('mylink');

} elseif($_GET['action'] == 'add') {
	
	$uid = $space[uid];
	$username = $space[username];
    $name = $_POST['name'];
	$url = $_POST['url'];
	$pic = '';
	$adddate = time();
	$query = $_SGLOBAL['db']->query("insert into  uchome_link values(null, '$uid', '$username', '$name', '$url', '$pic', '$adddate')");
	$lastId = $_SGLOBAL['db']-> insert_id();
	if ($lastId >0 ) {
		//个人动态前面的图标英文名，随意命名，需要修改css支持
		$icon = 'addlink';	
		//标题内容，支持html，其中 {actor} 为内置变量，表示当前人名
        $title_template = '{actor} 添加了新的 <a href="space.php?&do=mylink">友情链接</a>';	
        feed_add($icon, $title_template);
		getreward('mylink', 1, $uid, 0);
		$return = uc_pm_send(0, $space[uid], '友情链接系统信息发送测试', '你能收到系统信息吗？', 1, 0, 0);
		$return = uc_pm_send($space[uid], 3, '友情链接一般信息发送测试', '你能收到一般信息吗？', 1, 0, 0);
		$return = uc_pm_send(0, 0, '友情链接公共信息发送测试', '你能收到公共信息吗？', 1, 0, 0);
		notification_add(3, 'mylink', '友情链接发送通知测试');
		showmessage('添加友情链接成功', 'space.php?uid=$space[uid]&do=mylink');
	}else{
		showmessage('添加友情链接失败', 'space.php?uid=$space[uid]&do=mylink');
	}

} elseif($_GET['action'] == 'delete') {
	
    $id = $_GET['id'];
	$query = $_SGLOBAL['db']->query("delete  from uchome_link where id = '$id'");
	$rowsAffected = $_SGLOBAL['db']->affected_rows();
	if ($rowsAffected >0 ) {
		showmessage('删除友情链接成功', 'space.php?uid=$space[uid]&do=mylink');
	}else{
		showmessage('删除友情链接失败', 'space.php?uid=$space[uid]&do=mylink');
	}

} elseif($_GET['action'] == 'deleteall') {
	
    $id=$_POST['id'] ? $_POST['id'] : $_GET['id'];
    if ($_POST['id']) {
		$count=count($id);
		$str_id=$id[0];
		for ($i=1; $i < $count; $i++) {
              $str_id.=",".$id[$i];
	    }
    }else{
		$str_id=$id;
	}
	$query = $_SGLOBAL['db']->query("delete  from uchome_link where id in ($str_id)");
	$rowsAffected = $_SGLOBAL['db']->affected_rows();
	if ($rowsAffected >0 ) {
		showmessage('删除友情链接成功', 'space.php?uid=$space[uid]&do=mylink');
	}else{
		showmessage('删除友情链接失败', 'space.php?uid=$space[uid]&do=mylink');
	}

} elseif($_GET['action'] == 'edit') {
	$allowhtml = 1;
    $id = $_GET['id'];
	$query = $_SGLOBAL['db']->query("SELECT * FROM uchome_link WHERE id='$id'");
	$linkRs = $_SGLOBAL['db']->fetch_array($query);
	
	include template('mylink_me');
}else{


   include template('mylink');
}



?>